package odgs.app.server;

import java.text.NumberFormat;

import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import odgs.Contents;
import odgs.ContentsFile;
import odgs.Directory;
import odgs.ExplorerNode;
import odgs.GridState;
import odgs.LoginedGridNode;
import odgs.listener.ServerManagerListener;
import odgs.manager.ServerManager;

public class GridStateGraphController implements ServerManagerListener{
	private StateGraphUI ui;
	private ServerManager manager;
	private DefaultPieDataset data;

	public GridStateGraphController(StateGraphUI ui) {
		this.ui = ui;
		data = ui.getData();
	}

	public void setServerManager(ServerManager manager) {
		this.manager = manager;
		manager.addServerManagerListener(this);
		updateData();
	}

	private void updateData() {
		GridState state = manager.getGridState();
		
		data.setValue("Used", state.getUsedSize());
		data.setValue("Free", state.getNowCanUseSize());
		data.setValue("Can't use", state.getTotalSize() - state.getUsedSize()- state.getNowCanUseSize()); 

		/*
		NumberFormat nf = NumberFormat.getInstance();
		textPanel.canUseSize1.setText(nf.format(state.getNowCanUseSize())+ " Byte");
		textPanel.canUseSize2.setText(Util.getSizeFormat(state.getNowCanUseSize()));
		textPanel.usedSize1.setText(nf.format(state.getUsedSize()) + " Byte");
		textPanel.usedSize2.setText(Util.getSizeFormat(state.getUsedSize()));
		textPanel.totalSize1.setText(nf.format(state.getTotalSize())+ " Byte");
		textPanel.totalSize2.setText(Util.getSizeFormat(state.getTotalSize()));
		 */
	}

	public void loginedNode(String id) {
		updateData();
	}
	
	public void freeStorage(ContentsFile file) {
		updateData();
	}
	public void joinedNode(String id) {
		updateData();
	}

	public void useStorage(ContentsFile file) {
		updateData();
	}

	public void addedContents(Contents contents){
		updateData();
	}
	
	public void addedDirectory(Directory directory){
		updateData();
	}
	
	public void removeContents(Contents contents) {
		updateData();
	}
	
	public void removeDirectory(Directory directory) {
		updateData();
	}
	
	public void serverOpened(int port) {}
	public void error(int error){}
	public void log(String log) {}
	public void logout(String id)
	{
		updateData();
	}
	public void updatedExplorerNode(String mode, ExplorerNode node, String name) {}
}
